% MATLAB 示例：基于WMMSE优化波束赋形向量和人工噪声向量

% 初始化参数
K = 3; % 用户数量
C = 2; % 干扰数量
N = 4; % 天线数量
maxIter = 50; % 最大迭代次数
tolerance = 1e-6; % 收敛容忍度
P_T = 10; % 总功率约束
alpha = 0.5; % SCNR 和 SINR 的权重

% 随机生成信道矩阵
H = (randn(K, N) + 1j*randn(K, N)) / sqrt(2);
H_s = (randn(N, 1) + 1j*randn(N, 1)) / sqrt(2); % 感知信道
H_c = (randn(N, C) + 1j*randn(N, C)) / sqrt(2); % 干扰信道

% 初始化波束成形向量和人工噪声向量
W = randn(N, K) + 1j*randn(N, K);
Z = randn(N, 1) + 1j*randn(N, 1);

% 迭代优化
for iter = 1:maxIter
    % 更新波束成形向量
    cvx_begin
        variable W_new(N, K) complex
        variable Z_new(N, 1) complex
        variable t_w
        variable t_z
        % 计算 SCNR
        SCNR = abs(H_s' * W_new(:, 1))^2 / (sum(abs(H_c' * W_new).^2) + 1);
        % 计算 SINR
        SINR = zeros(K, 1);
        for k = 1:K
            SINR(k) = abs(H(k, :) * W_new(:, k))^2 / (sum(abs(H(k, :) * W_new).^2) - abs(H(k, :) * W_new(:, k))^2 + 1);
        end
        % 目标函数
        Objective = alpha * SCNR + (1 - alpha) * sum(SINR);
        maximize(Objective)
        subject to
            norm(W_new, 'fro')^2 + norm(Z_new, 'fro')^2 <= P_T;
            t_w >= norm(W_new);
            t_z >= norm(Z_new);
            t_w^2 + t_z^2 <= P_T;
    cvx_end
    
    % 检查收敛性
    if norm(W_new - W, 'fro') / norm(W, 'fro') < tolerance && norm(Z_new - Z, 'fro') / norm(Z, 'fro') < tolerance
        break;
    end
    
    % 更新波束成形向量和人工噪声向量
    W = W_new;
    Z = Z_new;
end

disp('Optimized Beamforming Matrix:');
disp(W);
disp('Optimized Artificial Noise Vector:');
disp(Z);
